package com.starview.star.operators.string.runtime;

import com.starview.platform.data.EvaluationException;
import com.starview.platform.data.IFunction;
import com.starview.platform.data.IValue;
import com.starview.platform.data.type.IType;
import com.starview.platform.data.type.StandardTypes;
import com.starview.platform.data.type.TypeUtils;
import com.starview.platform.data.value.Factory;
import com.starview.star.operators.CafeEnter;

/**
 * Runtime parts of the String2Number functions that parse strings into different kinds of numbers
 * 
 * @author fgm
 * 
 */
public class Char2Number
{
  public static class Char2Int implements IFunction
  {
    public static final String name = "__char_integer";

    @CafeEnter
    public static int enter(int ch)
    {
      return ch;
    }

    @Override
    public IType getType()
    {
      return type();
    }

    public static IType type()
    {
      return TypeUtils.functionType(StandardTypes.rawCharType, StandardTypes.rawIntegerType);
    }

    @Override
    public IValue enter(IValue... args) throws EvaluationException
    {
      return Factory.newInt(enter(Factory.charValue(args[0])));
    }
  }

  public static class Int2Char implements IFunction
  {
    public static final String name = "__integer_char";

    @CafeEnter
    public static int enter(int ch)
    {
      return ch;
    }

    @Override
    public IType getType()
    {
      return type();
    }

    public static IType type()
    {
      return TypeUtils.functionType(StandardTypes.rawIntegerType, StandardTypes.rawCharType);
    }

    @Override
    public IValue enter(IValue... args) throws EvaluationException
    {
      return Factory.newChar(enter(Factory.intValue(args[0])));
    }
  }
}
